namespace PickGold.Charting.Design
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Reflection;

    internal class HatchStyleEditor : UITypeEditor, IDisposable
    {
        private ChartGraphics _chartGraph;
        private bool _disposed;

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    this._chartGraph.Dispose();
                }
                this._disposed = true;
            }
        }

        ~HatchStyleEditor()
        {
            this.Dispose(false);
        }

        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override void PaintValue(PaintValueEventArgs e)
        {
            if (e.Value is ChartHatchStyle)
            {
                if (this._chartGraph == null)
                {
                    this._chartGraph = new ChartGraphics(null);
                }
                this._chartGraph.Graphics = e.Graphics;
                Color black = Color.Black;
                Color white = Color.White;
                if ((e.Context != null) && (e.Context.Instance != null))
                {
                    PropertyInfo property = e.Context.Instance.GetType().GetProperty("BackColor");
                    if (property != null)
                    {
                        black = (Color) property.GetValue(e.Context.Instance, null);
                    }
                    else
                    {
                        property = e.Context.Instance.GetType().GetProperty("BackColor");
                        if (property != null)
                        {
                            black = (Color) property.GetValue(e.Context.Instance, null);
                        }
                        else
                        {
                            property = e.Context.Instance.GetType().GetProperty("Color");
                            if (property != null)
                            {
                                black = (Color) property.GetValue(e.Context.Instance, null);
                            }
                        }
                    }
                    property = e.Context.Instance.GetType().GetProperty("BackSecondaryColor");
                    if (property != null)
                    {
                        white = (Color) property.GetValue(e.Context.Instance, null);
                    }
                    else
                    {
                        property = e.Context.Instance.GetType().GetProperty("BackSecondaryColor");
                        if (property != null)
                        {
                            white = (Color) property.GetValue(e.Context.Instance, null);
                        }
                    }
                }
                if (black == Color.Empty)
                {
                    black = Color.Black;
                }
                if (white == Color.Empty)
                {
                    white = Color.White;
                }
                if (black == white)
                {
                    white = Color.FromArgb(black.B, black.R, black.G);
                }
                if (((ChartHatchStyle) e.Value) != ChartHatchStyle.None)
                {
                    Brush brush = this._chartGraph.GetHatchBrush((ChartHatchStyle) e.Value, black, white);
                    e.Graphics.FillRectangle(brush, e.Bounds);
                    brush.Dispose();
                }
            }
        }
    }
}

